<template>
  <div class="login">
    <van-nav-bar class="bar" @click-left="$router.push('/home/find')">
      <template #left>
        <i class="iconfont f44">&#xe637; </i>
      </template>
    </van-nav-bar>
    <div class="content">
      <div class="tittle">您好，请登录</div>
      <van-form ref="form">
        <van-field
          name="mobile"
          v-model="form.mobile"
          placeholder="请填写手机号码"
          :rules="rules.mobile"
        >
          <template #left-icon>
            <i class="iconfont">&#xe60b;</i>
          </template></van-field
        >
        <van-field
          placeholder="请填写验证码"
          v-model="form.code"
          :rules="rules.code"
        >
          <template #left-icon>
            <i class="iconfont">&#xe60c;</i>
          </template>
          <template #button>
            <span class="code" v-if="totalTime === 6" @click="getCode"
              >获取验证码</span
            >
            <span class="code" v-else>{{ totalTime }}S后重置</span>
          </template>
        </van-field>
        <div class="other">
          登录即同意<span>《用户使用协议》</span>和<span>《隐私协议》</span>
        </div>
        <div>
          <van-button round block color="red" @click="submit">确定</van-button>
        </div>
      </van-form>
    </div>
  </div>
</template>
<script>
import { auCode, auLogin } from '@/api/login.js'
import { setLocal, getLocal } from '@/utils/local.js'
export default {
  data () {
    return {
      totalTime: 6,
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          { required: true, message: '请填写手机号码', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请正确输入手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ required: true, message: '请填写验证码', trigger: 'onChange' }]
      }
    }
  },
  created () {
    if (getLocal('token')) {
      this.$router.push('/home')
    }
  },
  methods: {
    submit () {
      this.$refs.form
        .validate()
        .then(() => {
          // window.console.log('成功')
          this.$refs.form
            .validate()
            .then(() => {
              auLogin(this.form).then(res => {
                console.log(res)
                setLocal('token', res.data.data.jwt)
                this.$store.commit('setUserInfo', res.data.data.user)
                this.$store.commit('setIsLogin', true)
                const _redirect = this.$route.query.redirect
                if (_redirect) {
                  this.$router.push(_redirect)
                } else {
                  this.$router.push('/home')
                }
                // console.log(_redirect)
              })
            })
            .catch(error => {
              console.log('验证错误:', error)
            })
        })
        .catch(() => {
          window.console.log('失败')
        })
    },
    getCode () {
      // console.log('123')
      this.$refs.form.validate('mobile').then(() => {
        this.totalTime--
        const _interval = setInterval(() => {
          this.totalTime--
          if (this.totalTime <= 0) {
            this.totalTime = 6
            clearInterval(_interval)
          }
        }, 1000)
        this.$toast.loading({ duration: 0 })
      })
      auCode({ mobile: this.form.mobile }).then(res => {
        window.console.log(res)
        this.$toast.success(res.data.data)
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login {
  padding: 0 @15;
  .f44 {
    font-size: 44px;
  }
  ::v-deep .van-nav-bar__left {
    padding: 0;
  }
  .content {
    .iconfont {
      padding-right: 10px;
    }
    .tittle {
      padding: 50px 0 62px;
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
    .code {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #00b8d4;
      line-height: 22.5px;
      letter-spacing: 0px;
    }
    .other {
      padding: 27px 0 44px;
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #181a39;
      line-height: 16.5px;
      letter-spacing: 0px;
      span {
        color: #00b8d4;
      }
    }
  }
}
</style>
